Skip to content

Fix float ghost default values - #539

Open
MohammedZaid1740 wants to merge 2 commits into
liquid-java:mainfrom
MohammedZaid1740:fix/float-ghost-default
Open

MohammedZaid1740 wants to merge 2 commits into
liquid-java:mainfrom
MohammedZaid1740:fix/float-ghost-default

Conversation

@MohammedZaid1740

Copy link
Copy Markdown

Description

Fixed the issue where declaring a float ghost caused the verifier to crash with the error "Ghost not implemented for type float".

Changes made:

  • Added 0.0 as the default value for float ghosts.
  • Added support for distinguishing 32-bit float from 64-bit double literals.
  • Updated Z3 translation to handle floating-point values with compatible precision.
  • Added a regression test for float ghost default values.

Example

import liquidjava.specification.Ghost;
import liquidjava.specification.StateRefinement;

@Ghost("float level")
class Tank {
    @StateRefinement(to = "true")
    public void fill() {}
}

A class with a float ghost can now be processed without the previous unsupported-type crash.

Related Issue

Closes #443

Type of change

  • Bug fix
  • New feature
  • Documentation update
  • Code refactoring

Checklist

  • Added/updated tests under liquidjava-example/src/main/java/testSuite/ (Correct* / Error*)
  • mvn test passes locally
  • Updated docs/README if behavior or API changed

@CatarinaGamboa CatarinaGamboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @MohammedZaid1740 thanks for your interest in contributing to our project!

I left a couple of comments in the PR.
The main issue is changing floats to 32-bit (FP32) since the verifier accept some programs it should reject, for example:

  1. 0.1f gets the wrong value. mkFP(0.1, FPSort32) gives 0.099999994, but Java's 0.1f is 0.10000000149 (why). So this wrong call is accepted:

    static void bad(@Refinement("x < 0.1") double x) {}
    float f = 0.1f; bad(f);
  2. Assignments like f += 0.1 break later checks. Java rounds the result to a float before storing it (JLS §5.1.3), but here it isn't rounded. No float can equal the unrounded value, so the verifier ends up with impossible facts and accepts everything after it:

    static void neg(@Refinement("_ < 0") float x) {}
    float f = 1.0f; f += 0.1; neg(f);   // accepted, but f is 1.1

So one option is to keep the floats as FP64 and maybe in a followup we can make more changes to use FP32 using the roundings on assigments and literals.

}

public Expr<?> makeFloatLiteral(double value) {
return z3.mkFP(value, z3.mkFPSort32());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since value is a double it won't convert well to Z3FP 32, convert the value first to float in Java, or just keep it FPSort64

}

public String toString() {
return Double.toString(value);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ignores is float so a message can look like found f == 0.1 but expected f == 0.1


LiteralReal other = (LiteralReal) obj;
return Double.doubleToLongBits(value) == Double.doubleToLongBits(other.value);
return Double.doubleToLongBits(value) == Double.doubleToLongBits(other.value) && isFloat == other.isFloat;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we want this?

import liquidjava.api.CommandLineLauncher;
import liquidjava.diagnostics.Diagnostics;

class TestFloatGhostDefault {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this can we have a test-suite file with a passing and failing test? Look at the examples in liquidjava-example/src/main/java/testSuite/. We can have like CorrectFloatGhost.java

@MohammedZaid1740

Copy link
Copy Markdown
Author

Thanks for the detailed review and suggestions!

I've updated the implementation based on your feedback:

  • Kept float values represented as FP64 for this PR to avoid incorrect FP32 conversions and rounding behavior.
  • Updated LiteralReal.toString() to distinguish float and double literals in diagnostic messages.
  • Updated equals() and hashCode() to compare numeric values consistently.
  • Replaced the standalone JUnit test with CorrectFloatGhost.java and ErrorFloatGhost.java in the existing test-suite format.

I reran TestExamples, and all 397 tests passed with 0 failures and 0 errors.

Thanks again for your guidance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A float ghost crashes the verifier: "Ghost not implemented for type float"

2 participants